package com.avcdata.aoscustommade.model;

import android.content.Context;

import com.avcdata.aoscustommade.activity.ForgetFindOrChangePassword_Activity;
import com.avcdata.aoscustommade.main.BaseCallBack;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 找回密码、修改密码的model
 * Created by zyh on 17/6/12.
 */

public class ForgetFindOrChangePasswordModel {

    private final Context mContext;

    public ForgetFindOrChangePasswordModel(Context context) {
        this.mContext = context;
    }

    /**
     * 验证手机号是否存在
     * @param isPhone
     * @param account
     * @param baseCallBack
     */
    public void loadCheackAccount(boolean isPhone, String account, final BaseCallBack baseCallBack) {
        String url = AvcConstants.URLHeaderUserinfo;
        if (isPhone) {
            url = url + "isExists?type=iphone&keyWord="+ account;
        } else {
            url = url + "isExists?type=email&keyWord="+ account;
        }
        AvcHttpUtils.post(url, new AvcJsonHttpResponseHandler(mContext){
            @Override
            public void onSuccess(JSONObject object) {
                super.onSuccess(object);
                baseCallBack.onSuccess(object);
            }

        });
    }

    /**
     * 账号重置
     * @param isPhone
     * @param name
     * @param password
     * @param baseCallBack
     */
    public void loadReset(boolean isPhone, String name, String password, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (isPhone) {
                jsonObject.put("oldUsername", name);
                jsonObject.put("password", password);

            } else {
                jsonObject.put("oldEmail", name);
                jsonObject.put("password", password);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "updateUsernameOrPassword", jsonObject, new AvcJsonHttpResponseHandler(mContext) {
            @Override
            public void onSuccess(JSONObject object) {
                super.onSuccess(object);
                baseCallBack.onSuccess(object);
            }
        });
    }
}
